<?php

require_once dirname(__FILE__).'/../lib/BasesfGuardRegisterActions.class.php';

/**
 * sfGuardRegister actions.
 *
 * @package    guard
 * @subpackage sfGuardRegister
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z jwage $
 */
class sfGuardRegisterActions extends BasesfGuardRegisterActions
{
	public function executeIndex(sfWebRequest $request)
	{
    	if ($this->getUser()->isAuthenticated()) {
      		$this->getUser()->setFlash('notice', 'You are already registered and signed in!');
      		$this->redirect('@homepage');
    	}

    	$this->form = new sfGuardRegisterForm();

    	if ($request->isMethod('post')) {
      		$this->form->bind($request->getParameter($this->form->getName()));
      		if ($this->form->isValid()) {
        		$user = $this->form->save();
        		$permission = new sfGuardUserPermission();
        		$permission->user_id = $user->id;
        		$permission->permission_id = 2; // use only customer permission
        		$permission->save();
        		$user_group = new sfGuardUserGroup();
        		$user_group->user_id = $user->id;
        		$user_group->group_id = 2; // set to customer group
        		$user_group->save();
        		$this->getUser()->signIn($user);

        		$this->redirect('@homepage');
      		}
    	}
  	}
}